package com.qr.order.controller;

import com.qr.order.command.OrderCommand;
import com.qr.order.entity.Product;
import com.qr.order.feign.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;


@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    //获取eureka中的信息的客户端
    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 基于Ribbon的形式调用
     *      客户端声明LoadBalance之后
     *      调用将ip和端口，直接替换为我们想要调用的服务名即可完成相应的调用
     */
    @RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
    public Product findByIdRibbon(@PathVariable Integer id) throws IOException {
        Product forObject = restTemplate.getForObject("http://service-product/product/1", Product.class);
        System.out.println(forObject);
        return forObject;
    }


    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    public Product getProduct(@PathVariable Integer id){
        return productService.findById(id);
    }

    /**
     * 调用线程池创建对象
     *      一个容器所能承载的并发量是有限，当请求过来的时候都在访问getProduct
     *      但是此时只有几个线程访问findByIdExecute,由于服务器线程都被getProduct
     *      导致我这个方法也不能执行，所以要进行线程隔离调用
     *
     *      定义一个OrderCommand这个类继承Hysix熔断器这个类
     *      然后构造方法中传入我们的参数
     *      静态方法设置一些参数，冲洗run方法，当我们执行这个线程的方法的时候
     *      就会执行其中的run方法
     *      从而将我们的所要发送的请求进行发送，，达到线程隔离的效果
     */
    @RequestMapping(value = "/execute/{id}",method = RequestMethod.GET)
    public Product findByIdExecute(@PathVariable Integer id) throws IOException {
        return new OrderCommand(restTemplate,id).execute();
    }



    /**
     * 根据id查询订单信息
     * @param id
     */
    /*@RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
    public Product findById(@PathVariable Integer id) throws IOException {
        //传入eureka中具体服务名称,获取服务的元数据
        List<ServiceInstance> instances = discoveryClient.getInstances("SERVICE-PRODUCT");

        for (ServiceInstance instance : instances) {
            System.out.println(instance);
        }

        ServiceInstance serviceInstance = instances.get(0);

        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
        //从eureka中拿到服务的主机和端口
        Product rest = restTemplate.getForObject("http://"+host+":"+port+"/product/1", Product.class);


        *//**
         * 如何通过order去调用product-service完成相关的查询
         *//*
        Product forObject = restTemplate.getForObject("http://localhost:9001/product/1", Product.class);
        System.out.println(forObject);
        return forObject;
    }
*/
}
